<?php


namespace app\common\cachemodel\live;


use app\chat\LiveMessage as ChatLiveMessage;
use app\common\lib\Util;
use app\common\model\BaseCache;
use think\Cache;
use XznsTools\cache\CacheManage;

class LiveStatusDataRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_status_data_record';

    protected $cacheModel = 'live.minute_status_data';

    /**
     * 获取直播间热度值
     * @param int $liveId 直播间ID
     * @return array
     * @throws \Exception
     */
    public function getHotNumData(int $liveId): array
    {
        try
        {
// 热度：热度值=实际在线人数+过去一小时内收到礼物的准币价值(取礼物产生热度值)+过去30分钟星座点赞数+过去10分钟弹幕量。
            // 实际在线人数
            $onlineUserNum = ChatLiveMessage::getClientIdCountByGroup($liveId . '_app_user_group');
            $giftHotNum = 0;    // 礼物热度值
            $zanNum = 0;        // 点赞数
            $commentNum = 0;    // 弹幕数
            for ($i = 0; $i < 60; $i++)
            {
                $time = date('YmdHi', strtotime("-{$i} minute"));

                $cacheData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $liveId . ':' . $time)->getCache();

                if ($cacheData)
                {
                    if ($i < 30)
                    {
                        // 30分钟内的星座点赞数
                        $zanNum += ($cacheData['zan_num'] ?? 0);
                        if ($i < 10)
                        {
                            // 10分钟内的弹幕数
                            $commentNum += ($cacheData['comment_num'] ?? 0);
                        }
                    }
                    // 1小时内的礼物热度数
                    $giftHotNum += ($cacheData['gift_hot_num'] ?? 0);
                }
            }
        }
        catch (\Exception $e)
        {
            $onlineUserNum = 0;
            $giftHotNum = 0;
            $zanNum = 0;
            $commentNum = 0;
        }

        return [
            'online_hot' => $onlineUserNum,
            'gift_hot' => $giftHotNum,
            'zan_hot' => $zanNum,
            'comment_hot' => $commentNum,
            'total_hot' => $onlineUserNum + $giftHotNum + $zanNum + $commentNum
        ];
    }

    /**
     * 获取每分钟 的 点赞数 评论数 礼物准币数 礼物热度数
     * @param $liveId
     * @param $date
     * @return array|bool|string
     * @throws \Exception
     */
    public function getMinuteDatas($liveId, $date)
    {
        $cacheKeyId = $liveId . ':' . $date;

        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $cacheKeyId)->getCache();
    }

    /**
     * 每分钟 点赞数 评论数 礼物准币数 礼物热度数 自增
     * @param int $liveId 直播间ID
     * @param $key // zan_num=>点赞数 comment_num=>评论数 gift_coin=>礼物价值（准币数） gift_hot_num=>礼物热度数
     * @param int $inc
     * @throws \Exception
     */
    public function setMinuteInc(int $liveId, $key, $inc = 1)
    {
        $time = time();
        $date = date('YmdHi', $time);
        $cacheKeyId = $liveId . ':' . $date;

        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $cacheKeyId)->setCacheInc($key, 'details', $inc);

        $isUpdateToData = Cache::get('live_cache_update_to_date:minute_status_data:' . $liveId, 0);

        if (!$isUpdateToData)
        {
            // 发送队列-》更新缓存内数据到数据库中
            Util::queuePush('live_cache_update_to_database', [
                'type' => 'minute_status_data',
                'data' => [
                    'time' => $time,
                    'live_id' => $liveId,
                ],
            ], 70);
            // 记录已添加同步缓存到数据库
            Cache::set('live_cache_update_to_date:minute_status_data:' . $liveId, 1, 86400);
        }
    }

    public function getLiveTotalData($liveRecordIds)
    {
        $data = $this->whereIn('live_record_id', $liveRecordIds)->field('sum(zan_hot) zan_hot_sum,sum(comment_hot) comment_hot_sum,sum(gift_hot) gift_hot_sum,sum(online_user_num) online_user_num_sum,avg(online_user_num) online_user_num_avg,max(online_user_num) online_user_num_max,sum(total_hot) total_hot_sum,avg(total_hot) total_hot_avg,max(total_hot) total_hot_max,sum(comment_num) comment_num_sum,sum(zan_num) zan_num_sum')->find();
        $data = json_decode(json_encode($data), true);
        if ($data && !is_null($data['total_hot_max']))
        {
            return [
                'zan_hot_sum' => $data['zan_hot_sum'],  // 点赞贡献热度值总量
                'comment_hot_sum' => $data['comment_hot_sum'],  // 弹幕贡献热度总量
                'online_user_num_sum' => $data['online_user_num_sum'],  // 在线用户贡献热度总数
                'online_user_num_avg' => $data['online_user_num_avg'],  // 在线用户均值
                'online_user_num_max' => $data['online_user_num_max'],  // 在线用户峰值
                'gift_hot_sum' => $data['gift_hot_sum'],    // 礼物热度总数
                'total_hot_sum' => $data['total_hot_sum'],  // 总热度
                'total_hot_avg' => $data['total_hot_avg'],  // 平均热度均值
                'total_hot_max' => $data['total_hot_max'],  // 热度峰值
                'comment_num_sum' => $data['comment_num_sum'],  // 弹幕次数总量
                'zan_num_sum' => $data['zan_num_sum'],  // 点赞次数总量
            ];
        } else
        {
            return [
                'zan_hot_sum' => 0,
                'comment_hot_sum' => 0,
                'online_user_num_sum' => 0,
                'online_user_num_avg' => 0,
                'online_user_num_max' => 0,
                'gift_hot_sum' => 0,
                'total_hot_sum' => 0,
                'total_hot_avg' => 0,
                'total_hot_max' => 0,
                'comment_num_sum' => 0,
                'zan_num_sum' => 0
            ];
        }
    }
}
